package com.szxy.hotel.controller;

import com.szxy.hotel.entity.Consume;
import com.szxy.hotel.entity.PageBean;
import com.szxy.hotel.entity.Room;
import com.szxy.hotel.service.IndexService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Resource(name = "indexService")
    IndexService indexService;

    /**
     * 主页房间列表
     */
    @RequestMapping("/indexList.do")
    @ResponseBody
    public Map<String,Object> indexList(int currentPage,String singleroom,String standardroom,String doubleroom,String deluxesuite,String presidentialsuite){
        Map<String, Object> map = new HashMap<String, Object>();
        PageBean<Room> pageBean = new PageBean<Room>();
        int pageSize = 8;
        int start = (currentPage -1) * pageSize;
        int size = pageSize;
        String roomState1 = "已预订";
        String roomState2 = "已入住";
        if((singleroom==null||singleroom=="")&&(standardroom==null||standardroom=="")&&(doubleroom==null||doubleroom=="")&&(deluxesuite==null||deluxesuite=="")&&(presidentialsuite==null||presidentialsuite=="")){
            List<Room> lists = indexService.findByPage1(start, size,roomState1,roomState2);
            List<Room> list = indexService.selectCount1(roomState1,roomState2);
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "没有数据");
                map.put("result", null);
            }else {
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }else {
            Map<String,Object> map1 = selectByRoom(currentPage,singleroom,standardroom,doubleroom,deluxesuite,presidentialsuite,pageBean,pageSize,start,size,roomState1,roomState2);
            map = map1;
        }
        return map;
    }

    /**
     * 主页房间列表的相关操作
     */
    private Map<String, Object> selectByRoom(int currentPage,String singleroom, String standardroom, String doubleroom, String deluxesuite, String presidentialsuite, PageBean<Room> pageBean, int pageSize, int start, int size, String roomState1, String roomState2) {
        Map<String, Object> map = new HashMap<String, Object>();
        if((singleroom!=null&&singleroom!="")&&(standardroom==null||standardroom=="")&&(doubleroom==null||doubleroom=="")&&(deluxesuite==null||deluxesuite=="")&&(presidentialsuite==null||presidentialsuite=="")){
            List<Room> lists = indexService.findByPageSingle(start, size,roomState1,roomState2,singleroom);
            List<Room> list = indexService.selectCountSingle(roomState1,roomState2,singleroom);
            int count = list.size();
            Map<String, Object> map2 = selectByRoomFinish(currentPage,lists,list,count,pageSize,pageBean);
            map = map2;
        }else if((singleroom==null||singleroom=="")&&(standardroom!=null&&standardroom!="")&&(doubleroom==null||doubleroom=="")&&(deluxesuite==null||deluxesuite=="")&&(presidentialsuite==null||presidentialsuite=="")){
            List<Room> lists = indexService.findByPageSingle(start, size,roomState1,roomState2,standardroom);
            List<Room> list = indexService.selectCountSingle(roomState1,roomState2,standardroom);
            int count = list.size();
            Map<String, Object> map2 = selectByRoomFinish(currentPage,lists,list,count,pageSize,pageBean);
            map = map2;
        }else if((singleroom==null||singleroom=="")&&(standardroom==null||standardroom=="")&&(doubleroom!=null&&doubleroom!="")&&(deluxesuite==null||deluxesuite=="")&&(presidentialsuite==null||presidentialsuite=="")){
            List<Room> lists = indexService.findByPageSingle(start, size,roomState1,roomState2,doubleroom);
            List<Room> list = indexService.selectCountSingle(roomState1,roomState2,doubleroom);
            int count = list.size();
            Map<String, Object> map2 = selectByRoomFinish(currentPage,lists,list,count,pageSize,pageBean);
            map = map2;
        }else if((singleroom==null||singleroom=="")&&(standardroom==null||standardroom=="")&&(doubleroom==null||doubleroom=="")&&(deluxesuite!=null&&deluxesuite!="")&&(presidentialsuite==null||presidentialsuite=="")){
            List<Room> lists = indexService.findByPageSingle(start, size,roomState1,roomState2,deluxesuite);
            List<Room> list = indexService.selectCountSingle(roomState1,roomState2,deluxesuite);
            int count = list.size();
            Map<String, Object> map2 = selectByRoomFinish(currentPage,lists,list,count,pageSize,pageBean);
            map = map2;
        }else if((singleroom==null||singleroom=="")&&(standardroom==null||standardroom=="")&&(doubleroom==null||doubleroom=="")&&(deluxesuite==null||deluxesuite=="")&&(presidentialsuite!=null&&presidentialsuite!="")){
            List<Room> lists = indexService.findByPageSingle(start, size,roomState1,roomState2,presidentialsuite);
            List<Room> list = indexService.selectCountSingle(roomState1,roomState2,presidentialsuite);
            int count = list.size();
            Map<String, Object> map2 = selectByRoomFinish(currentPage,lists,list,count,pageSize,pageBean);
            map = map2;
        }else {
            map.put("status", 1);
            map.put("message", "数据错误");
            map.put("result", null);
        }
        return map;
    }

    /**
     * 主页房间列表的相关操作完成
     */
    private Map<String, Object> selectByRoomFinish(int currentPage, List<Room> lists, List<Room> list, int count, int pageSize, PageBean<Room> pageBean) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (count <= 0) {
            map.put("status", 1);
            map.put("message", "没有数据");
            map.put("result", null);
        }else {
            int totalCount = count;
            double tc = totalCount;
            //总页数
            int totalPage = (int) Math.ceil(tc / pageSize);
            pageBean.setCurrentage(currentPage);
            pageBean.setPageSize(pageSize);
            pageBean.setTotalCount(totalCount);
            pageBean.setTotalPage(totalPage);
            pageBean.setLists(lists);
            map.put("status", 0);
            map.put("message", "有数据");
            map.put("result", pageBean);
        }
        return map;
    }

    /**
     * 主页消费列表
     */
    @RequestMapping("/indexConsumeList.do")
    @ResponseBody
    public Map<String,Object> indexConsumeList(int currentPage){
        Map<String, Object> map = new HashMap<String, Object>();
        PageBean<Consume> pageBean = new PageBean<Consume>();
        int pageSize = 6;
        int start = (currentPage -1) * pageSize;
        int size = pageSize;
        List<Consume> lists = indexService.findByPage(start, size);
        List<Consume> list = indexService.selectCount();
        int count = list.size();
        if (count <= 0) {
            map.put("status", 1);
            map.put("message", "没有数据");
            map.put("result", null);
        }else {
            int totalCount = count;
            double tc = totalCount;
            //总页数
            int totalPage = (int) Math.ceil(tc / pageSize);
            pageBean.setCurrentage(currentPage);
            pageBean.setPageSize(pageSize);
            pageBean.setTotalCount(totalCount);
            pageBean.setTotalPage(totalPage);
            pageBean.setLists(lists);
            map.put("status", 0);
            map.put("message", "有数据");
            map.put("result", pageBean);
        }
        return map;
    }
}
